<?php
/**
 * If you are not authorized to modify this file, do NOT touch it.
 * @file db.class.php
 * @author SkyWorld
 * @date 2011-1-17
 * @description This file is the object of database option
 */

class db{

	/**
	 *Those are private var of this object
     **/
	private $mysqlServerName = null;
	private $mysqlUserName = null;
	private $mysqlPassword = null;
	private $mysqlDatabase = null;
	private $mysqlPort = null;
	private $conn = null;

	/**
	 * @name  __construct
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  Constructor of db class, including connect to the database, select database, set char
     * @parameters  $mysqlServerName: server name of mysql
     * 				$mysqlUserName : username of database
     * 				$mysqlPassword : password of database
     *   			$mysqlDatabase : database name to connect
     * 				$mysqlPort     : port number of database
     * @return void
     **/
	public function __construct($mysqlServerName='localhost', $mysqlUserName='root', $mysqlPassword='root', $mysqlDatabase,$mysqlPort=3306){

		$this->conn = new mysqli($mysqlServerName, $mysqlUserName, $mysqlPassword,	$mysqlDatabase, $mysqlPort);

		if (mysqli_connect_errno()){
			die("Error<sup>[1]</sup> :Cannot connect to database.");
		}
		if (!$this->conn->set_charset("utf8")){
			die("Error<sup>[1]</sup>: Character set not found.");
		}
	}

	/**
	 * @name  __destruct
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  Destructor of db class, close the connection to database
     * @parameters  void
     * @return void
     **/
	public function __destruct(){
		$this->close();
	}

	/**
	 * @name  close
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  close the connection to database,if successed return true otherwise return false
     * @parameters  void
     * @return bool
     **/
	public function close(){
		return $this->conn->close();
	}


	/**
	 * @name  free_result
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  free resourse
     * @parameters  the result of SQL, if successed return ture, otherwise return false
     * @return bool
     **/
	public function free_result($query){
		$query->free_result();
	}

	/**
	 * @name  query
	 * @author SkyWorld
	 * @date 2011-9-17
	 * @description  exective SQL sentence, if successed return true, otherwise return false
     * @parameters  the SQL sentence to be exectived
     * @return bool
     **/
	public function query($sql){
		//return $query = $this->conn->query($sql);
		// following code just for testing while developing
		if($query = $this->conn->query($sql)) return $query;
		else
		{
			echo "<b>MySQL Error</b>: syntax error in sentence : {$sql}</br>";
			return false;
		}

	}


	/**
	 * @name  fetch_array
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  Return fetch result to an array,and than you can get the data by number index or string index
	 * 				 and at the same time the pointer refer to the next record
     * @parameters  retult of query
     * @return bool
     **/
	public function fetch_array($query, $result_type = MYSQLI_ASSOC){
		return $query->fetch_array($result_type);
	}

	/**
	 * @name  fetch_row
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  Return fetch result to an array,and than you can get the data by number index only
	 * 				 and at the same time the pointer refer to the next record
     * @parameters  retult of query
     * @return bool
     **/
	public function fetch_row($query, $result_type = MYSQLI_ASSOC){
		return $query->fetch_row($result_type);
	}

	/**
	 * @name  fetch_assoc
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  Return fetch result to an array,and than you can get the data by string index only
	 * 				 and at the same time the pointer refer to the next record
     * @parameters  retult of query
     * @return bool
     **/
	public function fetch_assoc($query,$result_type = MYSQLI_ASSOC){
		return $query->fetch_assoc($result_type);
	}

	/**
	 * @name  num_rows
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  Return the number of records
     * @parameters  retult of query
     * @return int
     **/
	public function num_rows($query){
		return $query->num_rows;
	}


	/**
	 * @name  insert_id
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  Return id of the last insertion
     * @parameters  void
     * @return int
     **/
	public function insert_id(){
		return $this->conn->insert_id;
	}


	/**
	 * @name  get_one
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  Get one record and return the result in the form of array
	 * 				 you can get the details by the number index or string index
     * @parameters  SQL sentences
     * @return array
     **/
	public function get_one($sql){
		$arr = array();
		$result = $this->query($sql);
		$arr = $this->fetch_array($result);
		$this->free_result($result);
		return $arr;
	}

	/**
	 * @name  get_one
	 * @author SkyWorld
	 * @date 2011-1-17
	 * @description  Get all records and return the result in the form of array
	 * 				 you can get the details by the number index or string index
     * @parameters  SQL sentences
     * @return array
     **/
	public function get_all($sql){
		$arr = array();
		$result = $this->query($sql);
		while ($row = $this->fetch_array($result)){
			$arr[]= $row;
		}
		$this->free_result($result);
		return $arr;
	}
}
?>